package com.gomcarter.frameworks.base.converter;

import com.gomcarter.frameworks.base.common.CustomLocalDateTimeUtils;
import org.apache.commons.lang3.StringUtils;

import java.beans.PropertyEditorSupport;
import java.time.Instant;
import java.time.ZoneId;

/**
 * Created by daibing on 2022/10/8.
 */
public class LocalDateEditor extends PropertyEditorSupport {
    public LocalDateEditor() {
        super();
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (StringUtils.isBlank(text)) {
            setValue(null);
        } else {
            if (StringUtils.isNumeric(text)) {
                setValue(Instant.ofEpochMilli(Long.parseLong(text)).atZone(ZoneId.systemDefault()).toLocalDate());
            } else {
                setValue(CustomLocalDateTimeUtils.fromString2LocalDate(text));
            }
        }
    }
}
